package com.daiyang.lottery.service.activitystatus.operater;

import com.daiyang.lottery.dao.dataobject.ActivityPrizeDO;
import com.daiyang.lottery.dao.mapper.ActivityPrizeMapper;
import com.daiyang.lottery.dao.mapper.PrizeMapper;
import com.daiyang.lottery.service.dto.ConvertActivityStatusDTO;
import com.daiyang.lottery.service.enums.ActivityPrizeStatusEnum;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PrizeOperator extends AbstractActivityOperator{

    @Autowired
    ActivityPrizeMapper activityPrizeMapper;
    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        Long prizeId = convertActivityStatusDTO.getPrizeId();
        ActivityPrizeStatusEnum targetPrizeStatus = convertActivityStatusDTO.getTargetPrizeStatus();
        if (activityId == null || prizeId == null||null==targetPrizeStatus) {
            return false;
        }
        ActivityPrizeDO activityPrizeDO = activityPrizeMapper.selectByAPId(activityId, prizeId);
        if(activityPrizeDO==null) {
            return false;
        }
        //判断当前奖品状态和目标是否一致

        if(targetPrizeStatus.name().equals(activityPrizeDO.getStatus())){
            return false;
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        Long prizeId = convertActivityStatusDTO.getPrizeId();
        ActivityPrizeStatusEnum targetPrizeStatus = convertActivityStatusDTO.getTargetPrizeStatus();
        try{
            activityPrizeMapper.upadteStatus(activityId,prizeId,targetPrizeStatus.name());
            return true;

        } catch (Exception e) {
            return false;
        }
    }

}
